package cate.game.activity.zonelimit.po;

import cate.common.table.activity.zonelimit.row.ZoneLimitBaseRow;
import cate.common.table.activity.zonelimit.row.ZoneLimitBoxRow;
import cate.common.table.activity.zonelimit.row.ZoneLimitItemRow;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.zonelimit.ZoneLimitHandler;
import cate.game.activity.zonelimit.msg.ZoneLimitResp;
import cate.game.activity.zonelimit.vo.ZoneLimitVO;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@NoteClass("玩家单个全民礼包数据")
public class RoleZoneLimit extends RoleActivitySingle {
    @NoteField(value = "本期购买状态记录", detail = "Map<档位,数量>")
    public Map<Integer, Integer> buyMap;

    @NoteField(value = "本期宝箱奖励领取状态", detail = "Map<奖励id,是否领取>")
    public Map<Integer, Boolean> rewardGot;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (buyMap == null) {
            buyMap = new HashMap<>();
        }
        if (rewardGot == null) {
            rewardGot = new HashMap<>();
        }
    }

    @Override
    public void excelCheck() {

    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
//        buyMap.clear();
//        if (!silence) {
//            noticeUpdate();
//        }
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        buyMap.clear();
        rewardGot.clear();
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @JsonIgnore
    public ZoneLimitBaseRow getRow() {
        return getRole().getGame().table.zoneLimit.base.get(base.configTid);
    }

    public void noticeUpdate() {
        role.sendNow(new ZoneLimitResp(buildVO()));
    }


    /**
     * 全服通知
     */
    public void noticeZone() {
        role.getGame().role.activeForOnline(role -> {
            if (!StringUtils.equals(role.getUid(), this.role.getUid())) {
                RoleZoneLimit r = role.getActivity().getEnableActivitySingle(funcId());
                if (r != null) {
                    r.noticeUpdate();
                }
            }
        });
    }

    public ZoneLimitVO buildVO() {
        ZoneLimitVO vo = new ZoneLimitVO();
        vo.role = this;
        ZoneLimitHandler handler = role.getGame().activityGlobal.getEnableHandler(funcId);
        if (handler != null) {
            vo.zone = handler.getGlobalData();
        }
        return vo;
    }


    @Override
    public GameResult<Void> allowPay(PayItemRow payItemRow) {
        GameResult<Void> r = new GameResult<>();
        ZoneLimitItemRow itemRow = role.getGame().table.zoneLimit.item.get(payItemRow.funcSubId);
        if (itemRow == null) {
            return r.fail("商品不存在");
        }
        if (getRow().itemGroup != itemRow.itemGroup) {
            return r.fail("商品分组有误");
        }
//        if (itemRow.day != getCurrentDay()) {
//            return r.fail("购买天数有误");
//        }
        int buyNum = buyMap.getOrDefault(itemRow.priceNo, 0);
        if (buyNum >= itemRow.limit) {
            return r.fail("已售罄");
        }
        if (StringUtils.isNotBlank(itemRow.m1Str)) {
            return r.fail("无法支付该商品");
        }
        ZoneLimitHandler enableHandler = role.getGame().activityGlobal.getEnableHandler(funcId());
        if (enableHandler == null) {
            return r.fail("找不到活动");
        }
        return enableHandler.getGlobalData().allowPay(itemRow);
    }


    @Override
    public void onPaySuccess(PayItemRow payItemRow) {
        ZoneLimitItemRow itemRow = role.getGame().table.zoneLimit.item.get(payItemRow.funcSubId);
        if (itemRow == null) {
            return;
        }
        new MixResAdder().setRes(new MixRes(itemRow.rwdStr)).setDialogReward(true).setOperation(GDOperation.ZONE_LIMT_BUY_RWD).setOperationSub(itemRow.id).exe(role);
//        if (itemRow.day == getCurrentDay()) {
        buyMap.put(itemRow.priceNo, buyMap.getOrDefault(itemRow.priceNo, 0) + 1);
//        }
        ZoneLimitHandler enableHandler = role.getGame().activityGlobal.getEnableHandler(funcId());
        if (enableHandler != null) {
            enableHandler.getGlobalData().onPaySuccess(itemRow);
            noticeUpdate();
            noticeZone();
        }
    }

    /**
     * 领取宝箱奖励
     *
     * @param rwdTid
     * @return
     */
    public GameResult<?> getBoxRwd(int rwdTid) {
        GameResult<?> r = new GameResult<>();
        if (rewardGot.containsKey(rwdTid)) {
            return r.fail("奖励已领取");
        }
        ZoneLimitBoxRow boxRow = role.getGame().table.zoneLimit.box.findRow(rwdTid, getRow().rwdGroup);
        if (boxRow == null) {
            return r.fail("奖励不存在");
        }
        ZoneLimitHandler enableHandler = role.getGame().activityGlobal.getEnableHandler(funcId());
        if (enableHandler == null) {
            return r.fail("活动不存在");
        }
        r = enableHandler.getGlobalData().allowGetBox(boxRow);
        if (!r.ok()) {
            return r;
        }
        rewardGot.put(rwdTid, true);
        EcResult exe = new MixResAdder().setRes(new MixRes(boxRow.rwdStr)).setDialogReward(true).setOperation(GDOperation.ZONE_LIMT_BOX_RWD).setOperationSub(boxRow.id).exe(role);
        if (!r.ok()) {
            return r.fail(exe);
        }
        noticeUpdate();
        return r;
    }

    public GameResult buyWithM1(int itemId) {
        GameResult r = new GameResult();
        ZoneLimitItemRow itemRow = role.getGame().table.zoneLimit.item.get(itemId);
        if (itemRow == null) {
            return r.fail("商品不存在");
        }
        if (getRow().itemGroup != itemRow.itemGroup) {
            return r.fail("商品分组有误");
        }
//        if (itemRow.day != getCurrentDay()) {
//            return r.fail("购买天数有误");
//        }
        int buyNum = buyMap.getOrDefault(itemRow.priceNo, 0);
        if (buyNum >= itemRow.limit) {
            return r.fail("已售罄");
        }
        ZoneLimitHandler enableHandler = role.getGame().activityGlobal.getEnableHandler(funcId());
        if (enableHandler == null) {
            return r.fail("找不到活动");
        }
        r = enableHandler.getGlobalData().allowPay(itemRow);
        if (!r.ok()) {
            return r;
        }
        if (StringUtils.isBlank(itemRow.m1Str)) {
            return r.fail("无法支付该商品");
        }
        MixRes consume = new MixRes(itemRow.m1Str);
        r = consume.consume(role, null);
        if (!r.ok()) {
            return r;
        }
        new MixResAdder().setRes(new MixRes(itemRow.rwdStr)).setDialogReward(true).setOperation(GDOperation.ZONE_LIMT_BUY_RWD).setOperationSub(itemRow.id).exe(role);
        enableHandler.getGlobalData().onPaySuccess(itemRow);
        buyMap.put(itemRow.priceNo, buyMap.getOrDefault(itemRow.priceNo, 0) + 1);
        noticeUpdate();
        noticeZone();
        return r.success();
    }
}
